<?php

use Biz\model\course\Course as mCourse;
use Biz\service\course\Course as sCourse;
use Doraemon\tools\Popup;

class ReceiveCourseAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;

    public function main()
    {
        $courseId = $this->getRequest()->getPost('course_id', 0);
        $info = mCourse::getByWhere(['course_id' => $courseId]);
        $isOwner = sCourse::isOwnerCourse($this->uid, $courseId);
        if ($isOwner && $info && $info['receive_link']) {
            $receiveLink = json_decode($info['receive_link'], true);
            $link = $receiveLink['link'];
            $pwd = $receiveLink['pwd'];
            $linkParam = ['type' => 'text', 'text' => $link, 'toast' => '已复制'];
            $link = 'niceliving://settings/copy?protego=' . urlencode(base64_encode(json_encode($linkParam, JSON_UNESCAPED_UNICODE)));
            $msg = [
                'type' => Popup::STYLE_PRAISE,
                'content' => [
                    'title' => '领取完整课件',
                    'desc' => '复制链接，用电脑浏览器打开即可下载' . ($pwd ? '，提取密码：' . $pwd : ''),
                    'img_url' => 'https://img.haohaozhu.cn//Op-imageShow//a41cd30au06o0Ys00qao8cs?iv=1',
                    'button_list' => [
                        [
                            'title' => '复制领取链接',
                            'link' => $link,
                            'background_color'=>'#57acb9',
                            'color'=>'#fafafa'
                        ]
                    ]
                ],
            ];
            $this->setSuccess((object)[], self::API_CODE_SUCCES, $msg);
        } else {
            throw new \Exception('你无法领取课件', -1);
        }

    }
}